//
//  ListenDetailController.m
//  LoveListen
//
//  Created by 王喆 on 15/7/3.
//  Copyright (c) 2015年 王喆. All rights reserved.
//

#import "ListenDetailController.h"
#import "DetailViewController.h"
#import "CollectionViewCell.h"
#import "ListenModel.h"
#define kUrl @"http://app.9nali.com/935/common_tag/4/%@?page_id=1&device=iPhone&version=1.1.5"
@interface ListenDetailController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    UICollectionView *_collectionView;
    
    
}

@property (nonatomic,strong)UICollectionView *collectionView;

@end

@implementation ListenDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    [self addTaskWithUrl:[NSString stringWithFormat:kUrl,self.str]];
    





}
-(void)addTaskWithUrl:(NSString *)url{
    
    _manager =[AFHTTPRequestOperationManager manager];
    _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"下载成功");
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
      //  NSDictionary *dict3 = dict[@"album"];
        NSMutableArray *arr = dict[@"list"];
        for (NSDictionary *dict2 in arr) {
            ListenModel *model = [[ListenModel alloc] init];
            [model setValuesForKeysWithDictionary:dict2];
         //   [model setValuesForKeysWithDictionary:dict];
            [self.dataArr addObject:model];
            
        }
        
        [self.collectionView reloadData];
        
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
        
    }];
    
    
}



-(void)creatView{
    _dataArr = [[NSMutableArray alloc]init];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((kScreenSize.width-30)/2, 180);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, kScreenSize.width, kScreenSize.height-64) collectionViewLayout: layout] ;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"collection"];
   // UIImageView *view=  [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"backImg1"]];
    //view.alpha = 0.5;
    self.collectionView.backgroundColor = [UIColor colorWithRed:211/255.0 green:226/255.0 blue:211/255.0 alpha:0.5];
    [self.view addSubview:self.collectionView];
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
    

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    //return [_dataArr[section]count];
    return _dataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    ListenModel *model = _dataArr[indexPath.row];
    [cell showDataWithModel:model indexPath:indexPath];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *detail = [[DetailViewController alloc] init];
    ListenModel *model = _dataArr[indexPath.row];
    detail.albumId = model.id;
  
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
